package com.jianglin.mapper;

import com.jianglin.model.Carcharge;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Component;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
@Component("carchargeDao")
@SuppressWarnings("SpellCheckingInspection")
public interface CarchargeMapper{

    @Results(id = "carchargeResultMap", value = {
            @Result(property = "id", column = "id", id = true),
            @Result(property = "payDate", column = "pay_date"),
            @Result(property = "endDate", column = "end_date"),
            @Result(property = "money", column = "money"),
            @Result(property = "status", column = "status"),
            @Result(property = "ownerId", column = "owner_id"),
            @Result(property = "remarks", column = "remarks"),
            @Result(property = "type", column = "type"),
            @Result(property = "parkId", column = "park_id"),
            @Result(property = "owner", column = "owner_id",
                    one = @One(select = "com.jianglin.mapper.OwnerMapper.findById", fetchType = FetchType.LAZY)),
            @Result(property = "parking", column = "park_id",
                    one = @One(select = "com.jianglin.mapper.ParkingMapper.findById", fetchType = FetchType.LAZY))
    })
    @Select("<script>" +
            "SELECT car.*, park.numbers, own.username " +
            "FROM carcharge car " +
            "LEFT JOIN parking park ON park.id = car.park_id " +
            "LEFT JOIN `owner` own ON own.id = car.owner_id " +
            "<where>" +
            "   <if test='parking != null and parking.numbers != null and parking.numbers != \"\"'>" +
            "       AND park.numbers = #{parking.numbers}" +
            "   </if>" +
            // 修改这里: 移除对空字符串的判断
            "   <if test='status != null'>" +
            "       AND car.status = #{status}" +
            "   </if>" +
            "   <if test='ownerId != null'>" +
            "       AND car.owner_id = #{ownerId}" +
            "   </if>" +
            "</where>" +
            "</script>")
    List<Carcharge> queryCarChargeAll(Carcharge carcharge);
    @Insert("INSERT INTO carcharge(pay_date, end_date, money, status, owner_id, remarks, type, park_id) " +
            "VALUES(#{payDate}, #{endDate}, #{money}, #{status}, #{ownerId}, #{remarks}, #{type}, #{parkId})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Carcharge carcharge);

    @Update("<script>" +
            "UPDATE carcharge " +
            "<set>" +
            "   <if test='payDate != null'>pay_date = #{payDate},</if>" +
            "   <if test='endDate != null'>end_date = #{endDate},</if>" +
            "   <if test='money != null'>money = #{money},</if>" +
            "   <if test='status != null'>status = #{status},</if>" +
            "   <if test='ownerId != null'>owner_id = #{ownerId},</if>" +
            "   <if test='remarks != null'>remarks = #{remarks},</if>" +
            "   <if test='type != null'>type = #{type},</if>" +
            "   <if test='parkId != null'>park_id = #{parkId},</if>" +
            "</set>" +
            "WHERE id = #{id}" +
            "</script>")
    int update(Carcharge carcharge);

    @Delete("DELETE FROM carcharge WHERE id = #{id}")
    int delete(Integer id); // 改为 Integer

    @Select("SELECT * FROM carcharge WHERE id = #{id}")
    @ResultMap("carchargeResultMap")
    Carcharge findById(Integer id); // 改为 Integer

}
